from redis.asyncio import Redis

from app.core.config import Settings
from app.schemas.auth import Captcha
from app.utils.captcha_utils import CaptchaUtils


class CaptchaService:
    """
    验证码服务类

    提供验证码生成和验证功能
    """

    def __init__(self, redis: Redis, settings: Settings):
        self.captcha_utils = CaptchaUtils(
            redis=redis,
            captcha_type=settings.CAPTCHA_TYPE,
            timeout=settings.CAPTCHA_TIMEOUT,
            font_path=settings.CAPTCHA_FONT_PATH,
            interference=settings.CAPTCHA_INTERFERENCE,
            key_prefix=settings.KEY_CAPTCHA_IMAGE_CODE,
        )

    async def generate_captcha(self) -> Captcha:
        """
        生成验证码图片和对应的验证码键

        返回:
            Captcha: 包含验证码图片和键的对象
        """
        return await self.captcha_utils.generate_captcha()

    async def verify_captcha(self, captcha_key: str, captcha_value: str) -> bool:
        """
        验证验证码是否正确

        参数:
            captcha_key (str): 验证码键
            captcha_value (str): 用户输入的验证码值

        返回:
            bool: 如果验证码正确则返回True，否则返回False
        """
        return await self.captcha_utils.verify_captcha(captcha_key, captcha_value)
